package net.mingsoft.basic.core;

import net.mingsoft.basic.constant.e.SessionConstEnum;
import net.mingsoft.basic.entity.ManagerSessionEntity;
import net.mingsoft.basic.util.BasicUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author gengfa
 * @date 22/01/14
 */
@Service
public class SessionContext {

    private static boolean mockEnabled;

    @Value("${ms.mock.enabled:false}")
    public void setMockEnabled(boolean mockEnabled) {
        SessionContext.mockEnabled = mockEnabled;
    }

    public static ManagerSessionEntity session() {
        if (mockEnabled) {
            return mockSession();
        }
        return (ManagerSessionEntity) BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION);
    }

    private static ManagerSessionEntity mockSession() {
        Object session = BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION);
        if (session == null) {
            ManagerSessionEntity entity = new ManagerSessionEntity();
            entity.setId("58");
            entity.setIntegerId(58);
            entity.setManagerNickName("张家辉");
            entity.setManagerName("张家辉");
            return entity;
        }
        return (ManagerSessionEntity) session;
    }
}